package com.meals.system.api.user;

import com.meals.system.api.user.param.SystemUserLoginParam;
import com.meals.system.api.user.param.SystemUserRegisterParam;
import com.meals.system.api.user.param.SystemUserUpdateParam;
import com.meals.system.api.user.vo.WhitesMealUserVO;
import com.meals.system.common.Constants;
import com.meals.system.common.ServiceResultEnum;
import com.meals.system.config.annotation.TokenToSystemUser;
import com.meals.system.entity.SystemUser;
import com.meals.system.service.WhitesMealUserService;
import com.meals.system.util.BeanUtil;
import com.meals.system.util.NumberUtil;
import com.meals.system.util.Result;
import com.meals.system.util.ResultGenerator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;


/**
 * Description:
 * Parm:
 * return:
 * Author: Chen
 * Date: 2021/12/5
 */

@RestController
@Api(value = "v1", tags = "3-1.白餐用户操作接口")
@RequestMapping("/user-api/v1")
public class WhitesMealUserAPI {

    @Resource
    private WhitesMealUserService whitesMealUserService;

    private static final Logger logger = LoggerFactory.getLogger(WhitesMealUserAPI.class);

    @PostMapping("/user/login")
    @ApiOperation(value = "登录接口", notes = "返回token")
    public Result<String> login(@RequestBody @Valid SystemUserLoginParam systemUserLoginParam){
        if (!NumberUtil.isPhone(systemUserLoginParam.getLoginName())){
            return ResultGenerator.genFailResult(ServiceResultEnum.LOGIN_NAME_IS_NOT_PHONE.getResult());
        }
        String loginResult = whitesMealUserService.login(systemUserLoginParam.getLoginName(),systemUserLoginParam.getPasswordMd5());

        logger.info("login api,loginName={}, loginResult={}",systemUserLoginParam.getLoginName(),loginResult);

        // 登录成功 Spring版本高 isEmpty已停用 hasLength试试看
        if (StringUtils.hasLength(loginResult) && loginResult.length() == Constants.TOKEN_LENGTH){
            Result result = ResultGenerator.genSuccessResult();
            result.setData(loginResult);
            return result;
        }
        // 登录失败
        return ResultGenerator.genFailResult(loginResult);
    }

    @PostMapping("/user/logout")
    @ApiOperation(value = "登出接口",notes = "清除token")
    public Result<String> logout(@TokenToSystemUser SystemUser loginSystemUser){
        Boolean logoutResult = whitesMealUserService.logout(loginSystemUser.getUserId());

        logger.info("logout api,loginSystemUser={}",loginSystemUser.getUserId());

        // 登出成功
        if (logoutResult) {
            return ResultGenerator.genSuccessResult();
        }
        // 登出失败
        return ResultGenerator.genFailResult("logout error");
    }

    @PostMapping("/user/register")
    @ApiOperation(value = "用户注册", notes = "")
    public Result register(@RequestBody @Valid SystemUserRegisterParam systemUserRegisterParam){
        if (!NumberUtil.isPhone(systemUserRegisterParam.getLoginName())){
            return ResultGenerator.genFailResult(ServiceResultEnum.LOGIN_NAME_IS_NOT_PHONE.getResult());
        }
        String registerResult = whitesMealUserService.register(systemUserRegisterParam.getLoginName(), systemUserRegisterParam.getPassword());
        logger.info("register api,loginName={},loginResult={}",systemUserRegisterParam.getLoginName(),systemUserRegisterParam.getPassword());

        // 注册成功
        if (ServiceResultEnum.SUCCESS.getResult().equals(registerResult)){
            return ResultGenerator.genSuccessResult();
        }
        // 注册失败
        return ResultGenerator.genFailResult(registerResult);
    }


    @PutMapping("/user/info")
    @ApiOperation(value = "修改用户信息", notes = "")
    public Result updateInfo(@RequestBody @ApiParam("用户信息")SystemUserUpdateParam systemUserUpdateParam, @TokenToSystemUser SystemUser loginSystemUser){
        Boolean flag = whitesMealUserService.updateUserInfo(systemUserUpdateParam, loginSystemUser.getUserId());
        if (flag){
            // 返回成功
            Result result = ResultGenerator.genSuccessResult();
            return result;
        } else{
            // 返回失败
            Result result = ResultGenerator.genFailResult("修改失败");
            return result;
        }
    }

    @GetMapping("/user/info")
    @ApiOperation(value = "获取用户信息", notes = "")
    public Result<WhitesMealUserVO> getSystemUserDetail(@TokenToSystemUser SystemUser loginSystemUser){
        // 已经登录则直接返回
        WhitesMealUserVO whitesMealUserVO = new WhitesMealUserVO();
        BeanUtil.copyProperties(loginSystemUser, whitesMealUserVO);
        return ResultGenerator.genSuccessResult(whitesMealUserVO);
    }


}
